package com.zenithmind.note.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.note.pojo.dto.NoteCommentDTO;
import com.zenithmind.note.pojo.entity.NoteComment;
import com.zenithmind.note.pojo.query.NoteCommentQuery;
import com.zenithmind.note.pojo.query.UserCommentQuery;
import com.zenithmind.note.pojo.vo.NoteCommentVO;
import com.zenithmind.note.service.NoteCommentService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 笔记评论控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/note-comment")
@RequiredArgsConstructor
@Tag(name = "笔记评论管理")
public class NoteCommentController {
    
    private final NoteCommentService noteCommentService;
    
    /**
     * 创建评论
     */
    @Log(description = "创建评论", operationType = OperationTypeEnum.ADD)
    @PostMapping("/createComment")
    @Operation(summary = "创建评论")
    public Result<NoteComment> createComment(@Validated @RequestBody NoteCommentDTO commentDTO) {
        log.info("创建评论：{}", commentDTO);
        return Result.success(noteCommentService.createComment(commentDTO));
    }
    
    /**
     * 删除评论
     */
    @Log(description = "删除评论", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/deleteComment/{commentId}")
    @Operation(summary = "删除评论")
    public Result<Void> deleteComment(@PathVariable String commentId) {
        log.info("删除评论：{}", commentId);
        noteCommentService.deleteComment(commentId);
        return Result.success();
    }
    
    /**
     * 获取笔记评论列表
     */
    @Log(description = "获取笔记评论列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getCommentsByNoteId/{noteId}")
    @Operation(summary = "获取笔记评论列表")
    public Result<List<NoteCommentVO>> getCommentsByNoteId(@PathVariable String noteId) {
        log.info("获取笔记评论列表：{}", noteId);
        return Result.success(noteCommentService.getCommentsByNoteId(noteId));
    }
    
    /**
     * 点赞/取消点赞评论
     */
    @Log(description = "点赞/取消点赞评论", operationType = OperationTypeEnum.UPDATE)
    @PostMapping("/toggleCommentLike/{commentId}")
    @Operation(summary = "点赞/取消点赞评论")
    public Result<Boolean> toggleCommentLike(@PathVariable String commentId) {
        log.info("点赞/取消点赞评论：{}", commentId);
        return noteCommentService.toggleCommentLike(commentId);
    }
    
    /**
     * 查询用户的评论列表
     */
    @Log(description = "查询用户的评论列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getUserComments")
    @Operation(summary = "查询用户的评论列表")
    public Result<PageResult<NoteCommentVO>> getUserComments(UserCommentQuery query) {
        log.info("查询用户的评论列表：{}", query);
        return Result.success(noteCommentService.getUserCommentPage(query));
    }
    
    /**
     * 分页查询评论列表（管理功能）
     */
    @Log(description = "分页查询评论列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/commentManagePage")
    @Operation(summary = "分页查询评论列表-管理功能")
    public Result<PageResult<NoteCommentVO>> commentManagePage(NoteCommentQuery query) {
        log.info("管理端分页查询评论列表：{}", query);
        return Result.success(noteCommentService.commentManagePage(query));
    }
    
    /**
     * 获取评论详情（管理功能）
     */
    @Log(description = "获取评论详情", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getCommentById")
    @Operation(summary = "获取评论详情-管理功能")
    public Result<NoteCommentVO> getCommentById(@RequestParam("commentId") String commentId) {
        log.info("管理端获取评论详情：{}", commentId);
        return Result.success(noteCommentService.getCommentById(commentId));
    }

    /**
     * 添加或修改评论（管理功能）
     */
    @Log(description = "添加或修改评论", operationType = OperationTypeEnum.UPDATE)
    @PostMapping("/addOrUpdateComment")
    @Operation(summary = "添加或修改评论-管理功能")
    public Result<NoteCommentVO> addOrUpdateComment(@RequestBody NoteComment comment) {
        log.info("添加或修改评论：{}", comment);
        return Result.success(noteCommentService.addOrUpdateComment(comment));
    }
    
    /**
     * 批量删除评论（管理功能）
     */
    @Log(description = "批量删除评论", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/deleteBatchComments")
    @Operation(summary = "批量删除评论-管理功能")
    public Result<Boolean> deleteBatchComments(@RequestBody List<String> ids) {
        log.info("批量删除评论：{}", ids);
        noteCommentService.deleteBatch(ids);
        return Result.success(true);
    }
    
    /**
     * 批量更新评论状态（管理功能）
     */
    @Log(description = "批量更新评论状态", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("/updateBatchCommentStatus")
    @Operation(summary = "批量更新评论状态-管理功能")
    public Result<Void> batchUpdateStatus(@RequestParam String ids, @RequestParam Integer status) {
        log.info("批量更新评论状态：ids={}, status={}", ids, status);
        List<String> idList = java.util.Arrays.stream(ids.split(","))
                .collect(java.util.stream.Collectors.toList());
        noteCommentService.batchUpdateStatus(idList, status);
        return Result.success();
    }
} 